//
//  PlayerViewController.m
//  Present
//
//  Created by dllo on 15/10/12.
//  Copyright (c) 2015年 Lemon. All rights reserved.
//

#import "PlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface PlayerViewController ()

@end

@implementation PlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //MPMovieControlStyleFullscreen//满屏
//    self.navigationController.navigationBarHidden = YES;//隐藏导航栏
//    self.navigationController.toolbarHidden = YES;//隐藏工具栏
    //[UIApplication sharedApplication].statusBarHidden = YES;//隐藏状态栏
    

//    //导航栏左侧按钮
//    UIImage *image = [UIImage imageNamed:@"iconfont-jiantouicon-1"];
//    //取消渲染
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)] autorelease];
    
    
    
    
    
    
    //系统提供的视频播放器
    MPMoviePlayerViewController *moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.videoModel.playUrl]];
    moviePlayerVC.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self addChildViewController:moviePlayerVC];
    [self.view addSubview:moviePlayerVC.view];
       //设定播放模式
    moviePlayerVC.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    
    
    //注册通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerVC.moviePlayer];
    
    
    
    
    //横屏
//    UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
//    [UIApplication sharedApplication].statusBarHidden = YES;
//    [[moviePlayerVC view] setFrame:CGRectMake(0, 0, keyWindow.frame.size.height, keyWindow.frame.size.width)];
//    [[moviePlayerVC view] setCenter: CGPointMake(keyWindow.center.x, keyWindow.center.y)];
//    [[moviePlayerVC view] setTransform:CGAffineTransformMakeRotation(M_PI_2)];
//    //[moviePlayerVC play];
//    [self.view addSubview: moviePlayerVC.view];
    
}

//当点击Done按键或者播放完毕时调用此函数
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
    
    MPMoviePlayerController* theMovie = [notify object];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
    
    [self dismissMoviePlayerViewControllerAnimated];
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    

    //结束模态
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}


//- (void)leftBarAction
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
